﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;

namespace BuildSql
{
    public class Program
    {

        #region SQL Table Definitions

        public const string CONST_PREFIX = "CONST:";

        public static string ConstantValue(string constValue)
        {
            return CONST_PREFIX + constValue;
        }

        public static bool TryGetConstantValue(string candidate, out string value)
        {
            if(candidate.StartsWith(CONST_PREFIX))
            {
                value = candidate.Substring(CONST_PREFIX.Length);
                return true;
            }
            value = candidate;
            return false;
        }

        private static SqlTable countryTable = new SqlTable
        (
            "Country",
            "wwiiol-meta/countries/country",
            new SqlColumn("ID", "INTEGER PRIMARY KEY AUTOINCREMENT", "@id"),
            new SqlColumn("Abbreviation", "varchar(255)", "abbr"),
            new SqlColumn("Name", "varchar(255)", "name")
        );

        private static SqlTable sideTable = new SqlTable
        (
            "Side",
            "wwiiol-meta/sides/side",
            new SqlColumn("ID", "INTEGER PRIMARY KEY AUTOINCREMENT", "@id"),
            new SqlColumn("Name", "varchar(255)", ".")
        );

        private static SqlTable cpTable = new SqlTable
        (
            "Towns",
            "cplist/cp",
            new SqlColumn("ID", "INTEGER PRIMARY KEY AUTOINCREMENT", "@id"),
            new SqlColumn("name", "varchar(255)", "@name"),
            new SqlColumn("type", "INTEGER", "@type"),
            new SqlColumn("orig_country", "INTEGER", "@orig-country"),
            new SqlColumn("orig_side", "INTEGER", "@orig-side"),
            new SqlColumn("links", "INTEGER", "@links"),
            new SqlColumn("x", "INTEGER", "@x"),
            new SqlColumn("y", "INTEGER", "@y"),
            new SqlColumn("curr_country", "INTEGER", "@orig-country"),
            new SqlColumn("curr_side", "INTEGER", "@orig-side"),
            new SqlColumn("scaledX", "REAL", "CONST:0"),
            new SqlColumn("scaledY", "REAL", "CONST:0")
        );

        #endregion

        static void Main(string[] args)
        {
            var metaDoc = LoadDoc("http://wiretap.wwiionline.com/wwiiol-meta.xml");
            var cpDoc = LoadDoc("http://wiretap.wwiionline.com/xml/cplist.xml");
            var linksDoc = LoadDoc("http://wiretap.wwiionline.com/xml/links.xml");

            StringBuilder output = new StringBuilder();
            output.Append(countryTable.GenerateSQL(metaDoc)).AppendLine();
            output.Append(sideTable.GenerateSQL(metaDoc)).AppendLine();
            output.Append(cpTable.GenerateSQL(cpDoc)).AppendLine();

            Console.WriteLine(output);
        }

        private static XmlDocument LoadDoc(string url)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(new XmlTextReader(url));
            return doc;
        }
    }
}
